package cool.lichuan.zabbix.util;

/**
 * @author lichuan
 * @date 2021/12/17 10:37
 * @description
 */
public class DateUtil {
    /**
     * 计算时间差
     *
     * @param startDate 开始时间戳
     * @param endDate   结束时间戳
     * @return 返回
     */
    public static String getDateDurations(Long startDate, Long endDate) {
        int step = 0;
        StringBuilder builder = new StringBuilder();
        if (null == startDate || 0 == startDate) {
            return "";
        }
        startDate *= 1000;
        if (null == endDate || 0 == endDate) {
            endDate = System.currentTimeMillis();
        } else {
            endDate *= 1000;
        }
        long nM = 1000L * 30 * 24 * 60 * 60;
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = Math.abs(endDate - startDate);
        if (diff < ns) {
            return diff + "毫秒";
        }
        // 计算差多少月
        long month = diff / nM;
        // 计算差多少天
        long day = diff % nM / nd;
        // 计算差多少小时
        long hour = diff % nM % nd / nh;
        // 计算差多少分钟
        long min = diff % nM % nd % nh / nm;
        // 计算差多少秒//输出结果
        long sec = diff % nM % nd % nh % nm / ns;
        if (month > 0) {
            step++;
            builder.append(month).append("月");
        }
        if (day > 0) {
            step++;
            builder.append(day).append("日");
        }
        if (hour > 0 || day > 0) {
            step++;
            builder.append(hour).append("时");
        }
        if (step < 3 && (min > 0 || day > 0 || hour > 0)) {
            step++;
            builder.append(min).append("分");
        }

        if (step < 3 && (sec > 0 || min > 0 || hour > 0)) {
            builder.append(sec).append("秒");
        }
        return builder.toString();
    }

    private DateUtil() {
    }
}
